package com.gitee.wsl.scheduler.job.runtime.scheduler

import com.gitee.wsl.logger.Logger
import com.gitee.wsl.logger.debug
import com.gitee.wsl.scheduler.job.repository.LockRepository
import com.gitee.wsl.scheduler.job.repository.sample.Memory
import com.gitee.wsl.concurrent.service.ScheduledExecutorService
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid

internal class KeepAliveScheduler(
    executorService: ScheduledExecutorService,
    period: Long,
    private val lockRepository: LockRepository =  LockRepository.Memory
) : SimplePeriodScheduler(executorService, period) {

    @OptIn(ExperimentalUuidApi::class)
    private suspend fun iAmAlive(id: Uuid) {
        lockRepository.ping(id)
    }

    @OptIn(ExperimentalUuidApi::class)
    fun start(id: Uuid): Unit = run {
        Logger.debug("Keep alive scheduled.")
        iAmAlive(id)
    }

}